﻿/*  
cd /projects/data_commons/lbd_code/
qsas lbd_1_raw.sas 8 &

Prepared by Feng Lin.

This script reads in the main LBD data and merge with fk_naics codes.
No data manipulation happens.
*/

libname llbd "/projects/data_commons/lbd/";
%Let dir_llbd = /projects/data_commons/lbd/;

/* 
--------------------------------------------------------------------------------
Macros that read raw LBD data and fk_naics
--------------------------------------------------------------------------------
*/

/*
--------------------------------------------------------------------------------
Macro that reads in a year
*/

%macro read_lbd(i_year= );

/* REDACTED
libname raw has been redacted */
libname raw "/REDACTED/";

%put ================================================================================;
%put Year &i_year.;
%put --------------------------------------------------------------------------------;

/* 
----------------------------------------
Specify the fk_naics variables to keep depending on year.
Note that earlier vintages of fk_naics are not available in later years.
*/

%if %eval(&i_year.)<=2009 %then %do;
  %Let naics_keep=lbdid fk_naics02 fk_naics07 fk_naics12;
%end;
%else %if %eval(&i_year.)<=2014 %then %do;
  %Let naics_keep=lbdid fk_naics07 fk_naics12;
%end;
%else %do;
  %Let naics_keep=lbdid fk_naics12;
%end;

/* 
----------------------------------------
Read data
*/

/* Main LBD */
data lbd_raw;
  set raw.lbd&i_year.c_c201600 (keep=firmnum lbdid cennum refnaics refsic st cty worker salary fullyr zipcode single);
run;

/* fk_naics */
data naics_raw;
  set raw.naics&i_year._c201600 (keep=&naics_keep.);
run;

/* 
----------------------------------------
Merge and append
*/

proc sort data=lbd_raw; by lbdid;
proc sort data=naics_raw; by lbdid;

%if &i_year. = 1976 %then %do;

data lbd;
  merge lbd_raw(in=in_main) naics_raw;
  by lbdid;
  if in_main=1;
run;

%end;

%else %do;

data lbd_raw;
  merge lbd_raw(in=in_main) naics_raw;
  by lbdid;
  if in_main=1;
run;

data lbd;
  set lbd lbd_raw;
run;

%end;

%mend;

/*
--------------------------------------------------------------------------------
Macro that loops over years
*/

%macro main_read_lbd();
  %Let i_year=1976;
  %do %while(%eval(&i_year.)<=2016);
    %read_lbd(i_year=&i_year.)
    %Let i_year = %eval(&i_year. + 1);
  %end;
%mend;

/* 
--------------------------------------------------------------------------------
Read raw LBD (run the macro above)
--------------------------------------------------------------------------------
*/


%main_read_lbd();

/* Save a copy of very raw data with no cleaning at all */
data llbd.lbd_raw_orig;
  set lbd;
run;

/* Save a copy of observerations with no firmnum */
data llbd.lbd_raw_firmnum_na;
  set lbd;
  if firmnum = .;
run;

proc export data=llbd.lbd_raw_firmnum_na outfile="&dir_llbd./lbd_raw_firmnum_na.dta" replace;
run;

/* End of sas file */
